# -*- coding:utf-8 -*-
"""Created By Corvo"""

from .....mdk.client.system.base import *
from .....mdk.ui.system.base import ModUIModuleBaseCls


# 提示结点
class TipNode(object):
    """提示结点"""
    __mVersion__ = 1

    def __init__(self, node_id, ui=None):
        self.id = node_id
        self.state = 0  # 结点状态
        self.child_on_num = 0  # 子节点状态为显示的数量
        self.children = {}  # 子节点
        self.parent = None  # 父节点
        self.bind_ui = ui  # 绑定ui

    # 更新结点
    def UpdateNode(self):
        """更新结点"""
        # print "[suc]", "UpdateNode:", self.id
        if not self.children:
            # 端点更新
            self.child_on_num = int(self.state > 0)
        else:
            self.child_on_num = 0
            for node in self.children.values():
                assert isinstance(node, TipNode)
                # 更新父节点显示数量
                self.child_on_num += node.child_on_num
            state = self.child_on_num > 0
            self.SetState(2 if state else 0, update=False)
        # 更新父节点
        if isinstance(self.parent, TipNode):
            self.parent.UpdateNode()
        # 更新显示
        self.UpdateBindUi()

    # 更新绑定的界面
    def UpdateBindUi(self):
        """更新绑定的界面"""
        system = MDKConfig.GetModuleClient()
        ui_node = system.GetUINode(self.bind_ui["ui_key"])  # type: ModUIModuleBaseCls
        if not ui_node:
            return

        path = self.bind_ui["path"]
        ui_node.SetVisible(path, self.state > 0)
        if self.state == 2:
            path += "/label"
            ui_node.SetLabelText(path, self.child_on_num).SetVisible(True)

    # 设置状态
    def SetState(self, state, update=True):
        # type: (int, bool) -> bool
        """
        设置状态\n
        - 0: 隐藏
        - 1: 显示显示
        - 2: 显示数字
        """
        state = max(0, min(2, state))
        if state == self.state:
            return False
        self.state = state
        if update:
            self.UpdateNode()
        return True

    # 获得状态
    def GetState(self):
        # type: () -> int
        """获得状态"""
        return self.state

    # 获得子结点启用状态数量
    def GetChildOnNum(self):
        # type: () -> int
        """获得子结点启用状态数量"""
        return self.child_on_num

    # 设置父节点
    def SetParent(self, node):
        # type: (TipNode) -> None
        """设置父节点"""
        self.parent = node

    # 添加子节点
    def AddChild(self, node):
        # type: (TipNode) -> None
        """添加子节点"""
        self.children[node.id] = node

    # 是否为端结点
    def IsEndNode(self):
        return self.children == {}


class TipUi(object):
    """提示ui"""
    __mVersion__ = 1

    def __init__(self):
        self.state = None  # 界面显示
        self.SetState(False)

    def SetState(self, state):
        if self.state == state:
            return
        self.state = state
        print "[suc]", "update", self.state
